"""
@file: shop_cart.py
@author: yangsiting
@data: 2021/04/28
@desc:
"""
from flask import request, g
from libs.nestable_blueprint import NestableBlueprint
from flask_restful import Api, Resource
from libs.handler import default_error_handler
from models.shop_cart import Shop_cart_Info
from models.goods import Goods_info
from models.base import db
from libs.response import generate_response
from serializer.shop_cart import shop_cart_schema,shops_cart_schema
from serializer.goods import good_schema

shop_cart_bp = NestableBlueprint('shop_cart_v1', __name__, url_prefix='shop_cart/')
# 定义restapi对象
api = Api(shop_cart_bp)

# 指定当出现异常时，所调用的处理方法
#. 当需要看到真实的错误信息时，请把下面这一句注释掉
#api.handle_error = default_error_handler

class AddgoodsView(Resource):
    def post(self):
        # 获取用户传过来的数据
        data = request.json
        goods_id = data.get("goods_id")
        user_id = data["user_id"]
        res = Shop_cart_Info.query.filter_by(goods_id=goods_id,user_profile_id=user_id).first()
        if not res:
            Shop_cart_Info.create_info(user_profile_id=data.get("user_id"),
                               goods_id=data.get("goods_id"),
                               goods_num=data.get("goods_nums"))
                               # total=data.get("total"))
            goods_info = Shop_cart_Info.query.filter_by(goods_id=data.get("goods_id")).first()
            result = shop_cart_schema.dump(goods_info)
            # # 返回结果
            return generate_response(data=result)
        else:
            res.goods_num = res.goods_num+data.get("goods_nums")
            db.session.commit()
            goods_info = Shop_cart_Info.query.filter_by(goods_id=data.get("goods_id")).first()
            result = shop_cart_schema.dump(goods_info)
            # # 返回结果
            return generate_response(data=result)

class QuerygoodsView(Resource):
    def get(self,user_id):
        goods = Shop_cart_Info.query.filter_by(user_profile_id=user_id).all()
        # goods_dict =shops_cart_schema(goods)
        goods_info = []
        for i in goods:
            good_dict = shop_cart_schema.dump(i)
            good_id = good_dict["goods_id"]
            goods_detail = Goods_info.query.filter_by(goods_id=good_id).first()
            goods_detail = good_schema.dump(goods_detail)
            good_dict.update(goods_detail)
            good_dict.update({"clickstatus":1})
            goods_info.append(good_dict)
        # 编写函数来进行序列化？ => 这一部分大家可以尽情优化~~
        return generate_response(data=goods_info)
        # return generate_response(data=servers_schema.dump(servers))

class AltergoodsView(Resource):
    # @auth.login_required
    def post(self):
        # 获取用户传过来的数据
        data = request.json
        goods_id = data.get("goods_id")
        res = Shop_cart_Info.query.filter_by(goods_id=goods_id).first()
        res.goods_num = data.get("goods_num")
        # res.total = data.get("total")
        db.session.commit()
        goods_info = Shop_cart_Info.query.filter_by(goods_id=data.get("goods_id")).first()
        result = shop_cart_schema.dump(goods_info)
        # # 返回结果
        return generate_response(data=result)

class DelgoodsView(Resource):
    # @auth.login_required
    def post(self):
        # 获取用户传过来的数据
        data = request.json
        goods_id = data.get("goods_id")
        res = Shop_cart_Info.query.filter_by(goods_id=goods_id).first()
        result = shop_cart_schema.dump(res)
        db.session.delete(res)
        db.session.commit()
        return generate_response(data=result)

class CleargoodsView(Resource):
    def post(self):
        data = request.json
        user_id = data.get("user_id")
        res = Shop_cart_Info.query.filter_by(user_profile_id=user_id).delete()
        db.session.commit()
        return generate_response(data = {"user_id":user_id,"message":"清空购物车中的数据"})


class ToOrderView(Resource):
    def post(self):
        data = request.json


api.add_resource(AddgoodsView, 'addgoods/')
api.add_resource(AltergoodsView,'altergoods/')
api.add_resource(QuerygoodsView, 'querygoods/<user_id>/')
api.add_resource(DelgoodsView,'delgoods/')
api.add_resource(CleargoodsView,'cleargoods/')